// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Spin Platform Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 62 : TYPE_SPINPLATFORM

// Function declarations
reserve function SpinPlatform_DebugDraw
reserve function SpinPlatform_DebugSpawn

// Static Values
public value SpinPlatform_value34 = 0;

// Tables
public table SpinPlatform_table43
	0, 1, 2, 3, 4, 3, 2, 1
end table

public table SpinPlatform_table42
	0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 3, 3, 1, 1, 1
	
end table


function SpinPlatform_DebugDraw
	DrawSprite(0)
end function


function SpinPlatform_DebugSpawn
	CreateTempObject(TypeName[Spin Platform], 0, object.xpos, object.ypos)
	object[tempObjectPos].value2 = object[0].value17
	object[tempObjectPos].value2 -= SpinPlatform_value34
	object[tempObjectPos].value2 <<= 4
	object[tempObjectPos].value2 += 16
	object[tempObjectPos].value2 <<= 2
	object[tempObjectPos].value2--
	object[tempObjectPos].drawOrder = 4
end function


event ObjectMain
	temp0 = SBZSetup_value31
	temp0 &= object.value2
	if temp0 == 0
		object.state = 1
		object.priority = PRIORITY_ACTIVE
	end if
	temp0 = 0
	if object.state != 0
		object.value0--
		if object.value0 < 0
			temp0 = 1
			object.value0 = object.value1
			object.state = 0
		end if
	end if
	if object.rotation != 0
		object.rotation++
		object.rotation &= 31
	else
		if temp0 != 0
			object.rotation++
		end if
	end if
	temp0 = object.rotation
	temp0 >>= 1
	GetTableValue(object.direction, temp0, SpinPlatform_table42)
	temp0 &= 7
	GetTableValue(object.frame, temp0, SpinPlatform_table43)
	if object.rotation == 0
		foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
			CheckEqual(object[currentPlayer].state, PlayerObject_HandleMonikaTeleport)
			temp7 = checkResult
			CheckLower(object[0].value1, 20)
			temp7 &= checkResult
			if temp7 == 0				
				BoxCollisionTest(C_BOX, object.entityPos, -16, -7, 16, 7, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			end if
			if object[currentPlayer].state == PlayerObject_HandleMonikaTeleport
				if object[0].value1 >= 14
					BoxCollisionTest(C_TOUCH, object.entityPos, -16, -7, 16, 7, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						if object[currentPlayer].ypos > object.ypos
							object[currentPlayer].iypos = object[currentPlayer].collisionBottom
							FlipSign(object[currentPlayer].ypos)
							object[currentPlayer].ypos += 0x70000
							object[currentPlayer].ypos += object.ypos
							if object[currentPlayer].yvel < 0
								object[currentPlayer].yvel = 0
							end if
						else
							object[currentPlayer].iypos = object[currentPlayer].collisionTop
							FlipSign(object[currentPlayer].ypos)
							object[currentPlayer].ypos -= 0x70000
							object[currentPlayer].ypos += object.ypos
							if object[currentPlayer].yvel > 0
								object[currentPlayer].yvel = 0
							end if
						end if		
					end if	
				end if	
			end if					
		next
	end if
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-16, -7, 32, 14, 413, 166)
	SpriteFrame(-16, -12, 32, 25, 446, 169)
	SpriteFrame(-15, -15, 30, 30, 480, 166)
	SpriteFrame(-13, -16, 25, 32, 449, 198)
	SpriteFrame(-7, -16, 14, 32, 480, 198)
	foreach (TypeName[Spin Platform], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].priority = PRIORITY_XBOUNDS
		object[arrayPos0].drawOrder = 4
		object[arrayPos0].value1 = object[arrayPos0].propertyValue
		object[arrayPos0].value1 &= 15
		object[arrayPos0].value1 *= 6
		object[arrayPos0].value0 = object[arrayPos0].value1
		object[arrayPos0].value2 = object[arrayPos0].propertyValue
		object[arrayPos0].value2 &= 112
		object[arrayPos0].value2 += 16
		object[arrayPos0].value2 <<= 2
		object[arrayPos0].value2--
	next
	temp0 = 0
	SpinPlatform_value34 = DebugMode_ObjCount
	while temp0 < 8
		SetTableValue(TypeName[Spin Platform], DebugMode_ObjCount, DebugMode_TypesTable)
		SetTableValue(SpinPlatform_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
		SetTableValue(SpinPlatform_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
		DebugMode_ObjCount++
		temp0++
	loop
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
